﻿using System;
using System.Windows.Input;

namespace Microsoft.Popfly.GameCreator.GameEngine.Events
{
    public class MouseLeaveEventChecker : IEventChecker
    {
        private Game m_game;
        private BehaviorOwner m_listenOn;
        private bool m_wasOutside;

        public MouseLeaveEventChecker(Game game, BehaviorOwner listenOn)
        {
            m_game = game;
            m_listenOn = listenOn;
            m_wasOutside = false;
            listenOn.AddMouseHoverEnterOrLeaveListener();
        }
        #region IEventChecker Members

        public bool FireThisFrame(double elapsedTimeInSeconds, out BehaviorOwner sender, out PGCEventArgs args)
        {
            MouseEventArgs mouseArgs = m_listenOn.HasMouseLeft();

            if (mouseArgs == null)
            {
                sender = null;
                args = null;
                m_wasOutside = false;
                return false;
            }
            else if (m_wasOutside)
            {
                sender = null;
                args = null;
                return false;
            }
            else
            {
                m_wasOutside = true;

                sender = m_listenOn;
                args = new PGCMouseEventArgs(m_game, mouseArgs);
                return true;
            }
        }

        public bool IsOfEventType(string eventType)
        {
            return eventType == "mouse";
        }

        #endregion
    }
}
